package com.anuo.app.common.globalhandle;


import com.anuo.app.common.utils.NetUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;

/**
 * 全局日志处理
 * 功能: 实现了日志中记录当前服务器Ip的功能, 方便集群排错, 请务必都用这个日志处理器来记录日志
 * 参考: http://www.cnblogs.com/zyfxlv/archive/2012/09/02/log4j.html
 */
public class GlobalLogHandler {
    private Logger logger;

    public GlobalLogHandler(Class clazz) {
        logger = LoggerFactory.getLogger(clazz);

    }

    public void debug(String msg) {
        MDC.put("localIp", NetUtil.getServerIp());
        logger.debug(msg);
    }

    public void debug(String msg, Throwable ex) {
        MDC.put("localIp", NetUtil.getServerIp());
        logger.debug(msg, ex);
    }

    public void error(String msg) {
        MDC.put("localIp", NetUtil.getServerIp());
        logger.error(msg);
    }

    public void error(String msg, Throwable ex) {
        MDC.put("localIp", NetUtil.getServerIp());
        logger.error(msg, ex);
    }
}
